//
//  HeaderMenuVIew.m
//  XinKaiFa55like
//
//  Created by 55like on 2017/9/25.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "HeaderMenuVIew.h"

@implementation HeaderMenuVIew
{
    UIView * menuLine;
    NSMutableArray * buttonArray;
    NSMutableArray * redviewArray;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        buttonArray = [[NSMutableArray alloc]init];
        self.titleArray = [[NSArray alloc]init];
        redviewArray = [[NSMutableArray alloc]init];
        
    }
    return self;
}
-(void)makeUI{
    
    
    CGFloat w = kScreenWidth/self.titleArray.count;
    
    for (int i = 0; i < self.titleArray.count; i ++) {
        
        NSString * title = self.titleArray[i];
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(w*i, 0, w, 45)];
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:rgbTitleDeepGray forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = Font(15);
        if (i == 0) {
             [button setTitleColor:rgbpublicColor forState:UIControlStateNormal];
        }
        [self addSubview:button];
        CGFloat tw = [title widthWithFont:15];
        UIView * redview = [[UIView alloc]initWithFrame:CGRectMake((w-tw)/2 + tw + 3, (45 - 15)/2 - 3, 5, 5)];
        redview.backgroundColor = [UIColor redColor];
        redview.hidden = YES;
        redview.layer.cornerRadius = redview.frameHeight/2;
        redview.clipsToBounds = YES;
        [button addSubview:redview];
        [buttonArray addObject:button];
        [redviewArray addObject:redview];
        
    }
    menuLine = [[UIView alloc]initWithFrame:CGRectMake(10, self.frameHeight - 1, w - 20, 1)];
    menuLine.backgroundColor = rgbpublicColor;
    [self addSubview:menuLine];
    
    
}
-(void)buttonClick:(UIButton *)button{
    
    for (int i = 0; i < buttonArray.count; i ++) {
        UIButton * btn = buttonArray[i];
        [btn setTitleColor:rgbTitleDeepGray forState:UIControlStateNormal];
        if (button == btn) {
            
            if ([self.delegate respondsToSelector:@selector(HeaderMenuVIewDidIndex:)]) {
                [self.delegate HeaderMenuVIewDidIndex:i];
 
            }
        }
    }
    
    [button setTitleColor:rgbpublicColor forState:UIControlStateNormal];

    [UIView animateWithDuration:0.3 animations:^{
       

        menuLine.frameX = button.frameX + 10;
        
        
    }];

}
-(void)setRedArray:(NSArray *)redArray{
    
    for (int i = 0; i < redArray.count; i ++) {
        
        UIView * redview = redviewArray[i];
        NSString * str = redArray[i];
        if ([str integerValue] > 0) {
            
            redview.hidden = NO;
            
        }else{
            
             redview.hidden = YES;
            
        }
         
        
    }
    
    
    
    
}
@end
